Conditions | 1 |
Paths | 8 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var Status = require('../Schema').OperationStatus |
||
50 | this.initialize = function () { |
||
51 | result.startedAt = new Date() |
||
52 | logger.info('Running initialization stage') |
||
53 | initialization |
||
54 | .initialize() |
||
55 | .then(function (value) { |
||
56 | if (value.status.successful) { |
||
57 | logger.info('Finished initialization stage') |
||
58 | } else { |
||
59 | logger.error('Initialization stage has failed:', value.error) |
||
60 | } |
||
61 | stageResults.initialization = value |
||
62 | logger.info('Running execution stage') |
||
63 | if (value.status.successful) { |
||
64 | return execution.run() |
||
65 | } else { |
||
66 | var message = 'Execution (scenario) stage is not run due to ' + |
||
67 | 'initialization stage fail' |
||
68 | logger.warn(message) |
||
69 | return null |
||
70 | } |
||
71 | }) |
||
72 | .then(function (value) { |
||
73 | stageResults.scenario = value |
||
74 | if (!value || value.status.successful) { |
||
75 | logger.info('Finished execution stage') |
||
76 | } else { |
||
77 | logger.error('Execution stage has failed:', value.value) |
||
78 | } |
||
79 | logger.info('Running termination stage') |
||
80 | return termination.run(stageResults.initialization, value) |
||
81 | }) |
||
82 | .then(function (value) { |
||
83 | stageResults.termination = value |
||
84 | if (value.status.successful) { |
||
85 | logger.info('Finished termination stage') |
||
86 | } else { |
||
87 | logger.error('Termination stage has failed:', value.value) |
||
88 | } |
||
89 | }) |
||
90 | .then(function () { |
||
91 | logger.info('Finished run') |
||
92 | var weights = [Status.Finished, Status.Failed, Status.Tripped] |
||
93 | result.status = Object.keys(stageResults).reduce(function (carrier, key) { |
||
94 | var item = stageResults[key] && stageResults[key].status |
||
95 | var weight = weights.indexOf(item) |
||
96 | return weight > weights.indexOf(carrier) ? item : carrier |
||
97 | }, Status.Finished) |
||
98 | }, function (reason) { |
||
99 | logger.info('Run has finished with unexpected error: {}', reason) |
||
100 | result.status = Status.Tripped |
||
101 | result.error = reason |
||
102 | }) |
||
103 | .then(function () { |
||
104 | result.finishedAt = new Date() |
||
105 | result.duration = result.finishedAt.getTime() - result.startedAt.getTime() |
||
106 | completion.resolve(result) |
||
107 | }) |
||
108 | return completion |
||
109 | } |
||
110 | |||
163 |